package com.dbn.webmagic.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dbn.webmagic.dao.PlantNameDao;
import com.dbn.webmagic.entity.Comnotice;
import com.dbn.webmagic.entity.PlantName;
import com.dbn.webmagic.utils.ResultData;
import com.dbn.webmagic.pageprocessor.NoticePipeline;
import com.dbn.webmagic.pageprocessor.NoticeProcessor;
import com.dbn.webmagic.service.ComnoticeService;
import com.dbn.webmagic.utils.UrlDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.model.HttpRequestBody;
import us.codecraft.webmagic.utils.HttpConstant;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liheping
 * @desciiption 爬虫接口
 * @date 2021/11/11
 */

@RestController
@RequestMapping("/notice")
@CrossOrigin
public class NoticeController {
    @Autowired
    private ComnoticeService comnoticeService;
    @Resource
    private NoticeProcessor noticeProcessor;
    @Autowired
    private PlantNameDao plantNameDao;
    @Resource
    private NoticePipeline noticePipeline;

    /*
    综合信息接口
     */
    @RequestMapping("/composite")
    public ResultData composite(@RequestParam(value = "name", required = false) String name//品种名称
            , @RequestParam(value = "sel1", required = false) String sel1//公告类型
            , @RequestParam(value = "sel2", required = false) String sel2//植物种类
            , @RequestParam(value = "sel3", required = false) String sel3//品种权事务子类型
            , @RequestParam(value = "per", required = false) String per) {//品种权人
        if(name==""&&per==""&&sel2==""){
            return ResultData.error(1,"请输入品种名称或植物种类或品种权人来进行查询");
        }
        if (name == null)
            name = "";
        if (per == null)
            per = "";
        if (sel1 == null)
            sel1 = "全部";
        if (sel2 == null)
            sel2 = "";
        if (sel3 == null)
            sel3 = "";

        if (sel2 != null && sel2 != "") {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("name", sel2);
            PlantName plantName = plantNameDao.selectOne(queryWrapper);
            if (plantName == null)
                return ResultData.error(1,"没有此作物");
            else
                sel2 = plantName.getLname();
        }
        List<Comnotice> comnoticeList=comnoticeService.selectbyparam(name,sel2,per);
        if(comnoticeList!=null){
            return ResultData.ok().put("comnoticeList",comnoticeList);
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sel2", sel2);
        params.put("sel1", sel1);
        params.put("name", name);
        params.put("sel3", sel3);
        params.put("per", per);
        String url = UrlDetail.comfirst_URL + per + "&sel1=" + sel1 + "&sel2=" + sel2 + "&sel3=" + sel3 + "&sel4=&t1=&t2=&t3=&t4=&pgs=15&nid=0&name=" + name + "&ad=&_=1635923968459";
        Request firstRequest = new Request(url);
        firstRequest.setMethod(HttpConstant.Method.GET);
        //用于网页之间传参
        firstRequest.setExtras(params);
        firstRequest.setRequestBody(HttpRequestBody.form(params, "utf-8"));
        //爬取结果
        Spider.create(noticeProcessor)
                .addRequest(firstRequest)
                .addPipeline(noticePipeline)
                .thread(5)
                .run();
        //爬取完毕，再次查询数据库是否添加内容
        List<Comnotice> comnoList = comnoticeService.selectbyparam(name, sel2, per);
        if (comnoList == null) {
            return ResultData.error(1,"暂无数据");
        }
        return ResultData.ok().put("comnoticeList", comnoList);
    }

    /*
     所有植物名爬取
      */
    @RequestMapping("/getPlant")
    public String getPlantName() {
        String url = UrlDetail.plant_url;
        Spider.create(noticeProcessor)
                .addUrl(url)
                .thread(5)
                .run();
        return "查询数据成功";
    }
}
